package com.haole.ecommercepay.refunds.service.impl;

import cn.hutool.http.HttpResponse;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.haole.ecommercepay.common.HttpUtils;
import com.haole.ecommercepay.refunds.entity.apply.RefundApplyParam;
import com.haole.ecommercepay.refunds.entity.query.QueryRefundsParam;
import com.haole.ecommercepay.refunds.service.RefundsService;
import com.haole.ecommercepay.refunds.url.RefundsUrl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

/**
 * @author haole
 */
@Service("refundsService")
public class RefundsServiceImpl implements RefundsService {

    private static final Logger logger = LoggerFactory.getLogger(RefundsServiceImpl.class);

    /**
     * 退款申请
     * @param param
     * @return
     */
    @Override
    public JSONObject refundApply(RefundApplyParam param) {
        String requestParam = JSON.toJSONString(param);
        logger.info("refunds apply param:{}",requestParam);
        HttpResponse response = HttpUtils.v3HttpExecute("POST", RefundsUrl.REFUND_APPLY,
                param.getAppName(),param.getMchId(),param.getMchSerialNo(),
                param.getWechatSerialNo(),requestParam,param.getMchPrivateKeyPath(),null);
        logger.info("refunds apply result:{}",response);
        return HttpUtils.httpResult(response,param.getWechatPubKeyPath());
    }
    /**
     * 通过微信支付退款单号查询退款
     * @param param
     * @return
     */
    @Override
    public JSONObject refundId(QueryRefundsParam param) {
        logger.info("refunds refundId param:{}",param);
        HttpResponse response = HttpUtils.v3HttpExecute("GET", String.format(RefundsUrl.REFUND_ID,param.getRefund_id(),param.getSub_mchid()),
                param.getAppName(),param.getMchId(),param.getMchSerialNo(),
                param.getWechatSerialNo(),"",param.getMchPrivateKeyPath(),null);
        logger.info("refunds refundId result:{}",response);
        return HttpUtils.httpResult(response,param.getWechatPubKeyPath());
    }
    /**
     * 通过商户退款单号查询退款
     * @param param
     * @return
     */
    @Override
    public JSONObject outRefundNo(QueryRefundsParam param) {
        logger.info("refunds outRefundNo param:{}",param);
        HttpResponse response = HttpUtils.v3HttpExecute("GET", String.format(RefundsUrl.OUT_REFUND_NO,param.getOut_refund_no(),param.getSub_mchid()),
                param.getAppName(),param.getMchId(),param.getMchSerialNo(),
                param.getWechatSerialNo(),"",param.getMchPrivateKeyPath(),null);
        logger.info("refunds outRefundNo result:{}",response);
        return HttpUtils.httpResult(response,param.getWechatPubKeyPath());
    }
}
